package com.base.controller.czb.czbapi;

import com.alibaba.fastjson.JSONObject;
import com.base.controller.api.RedisCacheManager;
import com.base.controller.utils.CZB.CZBTokenUtils;
import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Produce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/czbDetails")
@Api(tags = "油站详情")
public class CZB_Details {
    @Autowired
    UsersService usersService;
    @Autowired
    SubscribersService subscribersService;
    @Autowired
    RedisCacheManager redisCacheManager;


    @RequestMapping(value = "/getGasDetail", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "api接口", notes = "根据传递过来的数据油站详情")
    public @ResponseBody
    NetState getGasDetail(HttpServletRequest httpServletRequest, @ApiParam(value = "用户手机号") String mobile, @ApiParam(value = "商家parentId") String parentId, @ApiParam(value = "油站Ids") String gasIds) {
        NetState netState = new NetState();
        if (parentId == null || parentId.equals("")) {
            netState.setData("代理商ID必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (mobile == null || mobile.equals("")) {
            netState.setData("手机信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean isMobile = Produce.isMobile(mobile);
        if (!isMobile) {
            netState.setData("手机号码验证不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        if (gasIds == null || gasIds.equals("")) {
            netState.setData("加油站IDS信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        Users users = usersService.selectByPrimaryKey(parentId);
        int i=subscribersService.hasSub(mobile,parentId);
        List<Subscribers> sub = subscribersService.selectBySub(mobile, parentId);
        if (sub.size() < 1 || users == null) {
            netState.setData("用户信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Long Date = new Date().getTime();
        String token = CZBTokenUtils.getNowToken(mobile);
        String re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, token);
        JSONObject jsonObject = JSONObject.parseObject(re);
        String tokenNow = null;
        //请求过期，重新调用接口
        if (jsonObject.getString("code").equals("10013")) {
            tokenNow = CZBTokenUtils.getNowToken(mobile);
            re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, tokenNow);
            jsonObject = JSONObject.parseObject(re);
        }
        netState.setJson(jsonObject);
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setOtherJson(tokenNow);
        return netState;
    }
    /**
     *  @Date: 2020/4/26 9:59
     *  @Description:
     *  获取油站的全部详情信息
     */
    @RequestMapping(value = "/getGasDetailALL", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "api接口", notes = "根据传递过来的数据油站详情")
    public @ResponseBody
    NetState getGasDetailALL(HttpServletRequest httpServletRequest, @ApiParam(value = "用户手机号") @RequestParam String mobile, @ApiParam(value = "商家parentId") @RequestParam String parentId, @ApiParam(value = "油站Ids") @RequestParam String gasIds) {
        NetState netState = new NetState();
        if (parentId == null || parentId.equals("")) {
            netState.setData("代理商ID必须上传");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        if (mobile == null || mobile.equals("")) {
            netState.setData("手机信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }
        Boolean isMobile = Produce.isMobile(mobile);
        if (!isMobile) {
            netState.setData("手机号码验证不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        if (gasIds == null || gasIds.equals("")) {
            netState.setData("加油站IDS信息为空");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        Users users = usersService.selectByPrimaryKey(parentId);
        List<Subscribers> sub = subscribersService.selectBySub(mobile, parentId);
        if (sub.size() < 1 || users == null) {
            netState.setData("用户信息不正确");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        /**
         *  @Date: 2019/12/30 13:39
         *  @Description:
         *  返回数据类型
         */
        Map<Object, Object> map = redisCacheManager.hmget("GasList:" + gasIds);
        Long Date = new Date().getTime();
        String token = CZBTokenUtils.getNowToken(mobile);
        String re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, token);
        JSONObject jsonObject = JSONObject.parseObject(re);
        String tokenNow = null;
        //请求过期，重新调用接口
        if (jsonObject.getString("code").equals("10013")) {
            tokenNow = CZBTokenUtils.getNowToken(mobile);
            re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, tokenNow);
            jsonObject = JSONObject.parseObject(re);
        }
        netState.setJson(jsonObject);
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setOtherJson(map);
        return netState;
    }
}
